package com.jhgsys.internal.accident.controller;

import com.jhgsys.internal.accident.entity.AccidentReportFile;
import com.jhgsys.internal.accident.service.IAccidentReportFileService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 事故相关附件 Controller
 *
 * @author admin
 * @date 2024-08-07 16:27:37
 */
@Slf4j
@Validated
@Controller
public class AccidentReportFileController extends BaseController {

    @Autowired
    private IAccidentReportFileService accidentReportFileService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentReportFile")
    @RequiresPermissions("accidentReportFile:view")
    public String accidentReportFileIndex(){
        return FebsUtil.view("accidentReportFile/accidentReportFileList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentReportFile/add")
    @RequiresPermissions("accidentReportFile:add")
    public String accidentReportFileAdd(){
        return FebsUtil.view("accidentReportFile/accidentReportFileEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentReportFile/update/{id}")
    @RequiresPermissions("accidentReportFile:edit")
    public String accidentReportFileUpdate(Model model, @PathVariable("id") Integer id){
        model.addAttribute("accidentReportFile", accidentReportFileService.findAccidentReportFileById(id));
        return FebsUtil.view("accidentReportFile/accidentReportFileEdit");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-07 16:27:37
     */
    @GetMapping("accidentReportFile")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:view")
    public FebsResponse getAllAccidentReportFiles(AccidentReportFile accidentReportFile) {
        return new FebsResponse().success().data(accidentReportFileService.findAccidentReportFiles(accidentReportFile));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-07 16:27:37
     */
    @GetMapping("accidentReportFile/list")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:view")
    public FebsResponse accidentReportFileList(QueryRequest request, AccidentReportFile accidentReportFile) {
        Map<String, Object> dataTable = getDataTable(this.accidentReportFileService.findAccidentReportFiles(request, accidentReportFile));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 AccidentReportFile
     * @author admin
     * @date 2024-08-07 16:27:37
     */
    @Log("新增AccidentReportFile")
    @PostMapping("accidentReportFile")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:add")
    public FebsResponse addAccidentReportFile(@Valid AccidentReportFile accidentReportFile) throws FebsException {
        try {
            accidentReportFile.setCreatedBy(getCurrentUser().getUserName());
            this.accidentReportFileService.createAccidentReportFile(accidentReportFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增AccidentReportFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 AccidentReportFile
     * @author admin
     * @date 2024-08-07 16:27:37
     */
    @Log("逻辑删除AccidentReportFile")
    @GetMapping("accidentReportFile/delete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:delete")
    public FebsResponse deleteAccidentReportFile(@PathVariable("id") String accidentReportFileIds) throws FebsException {
        try {
            AccidentReportFile accidentReportFile = new AccidentReportFile();
            accidentReportFile.setDeletedBy(getCurrentUser().getUserName());
            accidentReportFile.setDeletedTime(new Date());
            accidentReportFile.setDeletedFlg(FebsConstant.INVALID);
            this.accidentReportFileService.deleteAccidentReportFile(accidentReportFile,accidentReportFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentReportFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 AccidentReportFile
    * @author admin
    * @date 2024-08-07 16:27:37
    */
    @Log("物理删除AccidentReportFile")
    @GetMapping("accidentReportFile/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:delete")
    public FebsResponse physicallyDeleteAccidentReportFile(@PathVariable("id") String accidentReportFileIds) throws FebsException {
        try {
            this.accidentReportFileService.physicallyDeleteAccidentReportFile(accidentReportFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentReportFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 AccidentReportFile
     * @author admin
     * @date 2024-08-07 16:27:37
     */
    @Log("修改AccidentReportFile")
    @PostMapping("accidentReportFile/update")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:edit")
    public FebsResponse updateAccidentReportFile(AccidentReportFile accidentReportFile) throws FebsException {
        try {
            accidentReportFile.setUpdatedBy(getCurrentUser().getUserName());
            this.accidentReportFileService.updateAccidentReportFile(accidentReportFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改AccidentReportFile失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 AccidentReportFile的excel
     * @author admin
     * @date 2024-08-07 16:27:37
     */
    @PostMapping("accidentReportFile/excel")
    @ResponseBody
    @RequiresPermissions("accidentReportFile:view")
    public void export(QueryRequest queryRequest, AccidentReportFile accidentReportFile, HttpServletResponse response) throws FebsException {
        try {
            List<AccidentReportFile> accidentReportFiles = this.accidentReportFileService.findAccidentReportFiles(queryRequest, accidentReportFile).getRecords();
            ExcelKit.$Export(AccidentReportFile.class, response).downXlsx(accidentReportFiles, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
